#include "port_menu.h"

Port_Menu::Port_Menu(QWidget *parent) : QMainWindow(parent)
{
    Sysctrl->SYS_CTL_MODE =0;
    this->setStyleSheet("QMainWindow{background-image: url(:/pic/背景板.png);}");
    this->setFixedSize(640,430);
    timer = new QTimer(this);
    timer->start(1000);

    UI_Init();
    connect_Init();

}

void Port_Menu::UI_Init()
{

    the_btn_style[0]=("\
                      outline: 0px ;                            /* 去掉获取焦点时的虚线 */ \
                      background-color: rgb(0, 170, 255);	    /* 背景颜色 */\
                      border-radius: 6px;		                 /* 按钮边框的圆角设置 */\
                      border-style: outset;\
                      /* 按钮背景图标设置 */\
                      background-image: url(:/pic/access/气象站.png); /* 背景图片 */\
                      background-origin: content;\
                      background-position: left;	         /* 背景图片的位置 */\
                      padding-right: 0px;	    /*内边距设置*/	    \
                      padding-left: 10px;		/*  */	\
                      padding-bottom: 10px;		/*  */    \
                      padding-top: 10px;		/*  */    \
                      background-repeat: no-repeat;	         /* 设置背景图像的平铺模式 */\
                      /* 按钮文本设置 */\
                      text-align: content;			             /* 文本的位置 */\
                      font-size: 18px;\
                      color: #FFFFFF;			                 /* 文本颜色 */\
                 "
);

//
the_btn_style[2]=("QPushButton:pressed{background-color:rgb(99,211,133);}");
the_btn_style[1]=("\
                  outline: 0px ;                            /* 去掉获取焦点时的虚线 */ \
                  border-radius: 8px;		                 /* 按钮边框的圆角设置 */\
                  border-style: outset;\
                  /* 按钮背景图标设置 */\
                  background-image: url(:/pic/Airflow_mode/等待.png); /* 背景图片 */\
                  background-origin: content;\
                  background-position: left;	         /* 背景图片的位置 */\
                  padding-right: 0px;	    /*内边距设置*/	    \
                  padding-left: 10px;		/*  */	\
                  padding-bottom: 10px;	/*  */   \
                  padding-top:10px;		/*  */    \
                  background-repeat: repeat;	         /* 设置背景图像的平铺模式 */\
                  /* 按钮文本设置 */\
                  text-align:content ;			             /* 文本的位置 */\
                  font-size: 22px;\
                  color: rgb(225, 225, 225);			                 /* 文本颜色 */\
                  ");

                  for(int i=0;i<7;i++)//创建主屏幕显示的按键
                  {
                       theBtn[i] = new QPushButton(this);
              //        newbutton->setGeometry(30+(i*150),260,120,90 );//设定每隔放置一个按键
                      theBtn[i]->setFixedSize(150,40);
                      theBtn[i]->geometry();
                      btnlist_ui.append(theBtn[i]);
                  }
                  theBtn[0]->setStyleSheet(  "QPushButton{"+the_btn_style[0]+"background-color: rgb(255, 150, 55);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/background/GESYTEC系列号.png); /* 背景图片 */"+"}"
                   "QPushButton:focus{"+the_btn_style[1]+"background-color: rgb(255, 50, 55);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/background/GESYTEC系列号.png); /* 背景图片 */"+"}");
                  theBtn[1]->setStyleSheet(  "QPushButton{"+the_btn_style[0]+"background-color: rgb(255, 150, 55);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/background/TCP IP设置.png); /* 背景图片 */"+"}"
                   "QPushButton:focus{"+the_btn_style[1]+"background-color: rgb(255, 50, 55);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/background/TCP IP设置.png); /* 背景图片 */"+"}");
                  theBtn[2]->setStyleSheet(  "QPushButton{"+the_btn_style[0]+"background-color: rgb(255, 150, 55);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/background/仪器识别号.png); /* 背景图片 */"+"}"
                   "QPushButton:focus{"+the_btn_style[1]+"background-color: rgb(255, 50, 55);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/background/仪器识别号.png); /* 背景图片 */"+"}");
                  theBtn[3]->setStyleSheet(  "QPushButton{"+the_btn_style[0]+"background-color: rgb(255, 150, 55);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/background/数据流配置.png); /* 背景图片 */"+"}"
                   "QPushButton:focus{"+the_btn_style[1]+"background-color: rgb(255, 50, 55);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/background/数据流配置.png); /* 背景图片 */"+"}");
                  theBtn[4]->setStyleSheet(  "QPushButton{"+the_btn_style[0]+"background-color: rgb(255, 150, 55);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/background/通讯协议.png); /* 背景图片 */"+"}"
                   "QPushButton:focus{"+the_btn_style[1]+"background-color: rgb(255, 50, 55);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/background/通讯协议.png); /* 背景图片 */"+"}");
                  theBtn[5]->setStyleSheet(  "QPushButton{"+the_btn_style[0]+"background-color: rgb(255, 150, 55);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/background/串口设置.png); /* 背景图片 */"+"}"
                   "QPushButton:focus{"+the_btn_style[1]+"background-color: rgb(255, 50, 55);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/background/串口设置.png); /* 背景图片 */"+"}");
                  theBtn[6]->setStyleSheet(  "QPushButton{"+the_btn_style[0]+"background-color: rgb(255, 150, 55);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/返回.png); /* 背景图片 */"+"}"
                   "QPushButton:focus{"+the_btn_style[1]+"background-color: rgb(255, 50, 55);	    /* 背景颜色 */ ;"+ "background-image: url(:/pic/返回2.png); /* 背景图片 */"+"}");

                 btnlist_ui.at(0)->setText(tr("     波特率"));
                 btnlist_ui.at(1)->setText(tr("     数据位"));
                 btnlist_ui.at(2)->setText(tr("     奇偶校准"));
                 btnlist_ui.at(3)->setText(tr("     停止位"));
                 btnlist_ui.at(4)->setText(tr("     协议选择"));
                 btnlist_ui.at(5)->setText(tr("     通讯协议"));
                 btnlist_ui.at(6)->setText(tr("     返回"));


                  for(int i=0;i<9;i++)//创建主屏幕显示的按键
                  {
                       theLabel[i] = new QLabel(this);
                       theLabel[i]->setStyleSheet("QLabel{\
                                              outline: 0px ;                            /* 去掉获取焦点时的虚线 */ \
                                              border-radius: 6px;		                 /* 按钮边框的圆角设置 */\
                                              border-style: outset;\
                                              /* 按钮文本设置 */\
                                              qproperty-alignment: 'AlignCenter| AlignCenter';			             /* 文本的位置 */\
                                              font-size: 20px;\
                                              color: #123567;			                 /* 文本颜色 */\
                                         }"
                        );
                  }
                  theLabel[0]->setStyleSheet("QLabel{ image: url(:/pic/logo.png); background-color: rgb(20, 170, 225);}"    );
                  theLabel[0]->setStyleSheet("QLabel{"
                                        + the_StyleSheet[20]+ " font-size: 25px;"+ "background-color: rgb(20, 70, 255);	    /* 背景颜色 */"+  "}"    );
                  theLabel[1]->setStyleSheet("QLabel{ image: url(:/pic/左.png); }"    );
                  theLabel[2]->setStyleSheet("QLabel{ image: url(:/pic/右.png); }"    );

                 theLabel[0]->setGeometry(0,0,640,60);//设定每隔放置一个按键
                 theLabel[1]->setGeometry(0,0,300,60);//设定每隔放置一个按键
                 theLabel[2]->setGeometry(340,0,300,60);//设定每隔放置一个按键
                 theLabel[0]->setText("串口设置");

                 switch (Sysset->CommuSet.Serial_Opt.boundrate)
                 {
                 case 2400:
                 {
                     flag_bundrate=0;//1->4800 2->9600  3->19200  4->38400 5->57600  6->115200
                     break;
                 }
                 case 4800:
                 {
                     flag_bundrate=1;
                     break;
                 }

                 case 9600:
                 {
                     flag_bundrate=2;
                     break;
                 }

                 case 19200:
                 {
                     flag_bundrate=3;
                     break;

                 }
                 case 38400:
                 {
                     flag_bundrate=4;
                     break;
                 }
                 case 57600:
                 {
                     flag_bundrate=5;
                     break;
                 }

                 case 115200:
                 {
                     flag_bundrate=6;
                     break;
                 }
                 default:
                     break;
                 }

                 //波特率
                 theLabel[3]->setText(bundrate_sel[flag_bundrate]);
                 //数据位
                  theLabel[4]->setText(QString::number(Sysset->CommuSet.Serial_Opt.databit));
                 //奇偶校验
                  theLabel[5]->setText((parity_sel[Sysset->CommuSet.Serial_Opt.parity]));
                 //停止位
                  theLabel[6]->setText(QString::number(Sysset->CommuSet.Serial_Opt.stopbit));
                 //协议
                  qDebug()<<Sysset->CommuSet.Com_Bus_Sel;
                  theLabel[7]->setText(XY_sel[Sysset->CommuSet.Com_Bus_Sel]);

                  if(Sysset->CommuSet.communicate_procotol==0)
                  {
                      theLabel[8]->setText("modbus");
                  }
                  else
                  {
                      theLabel[8]->setText("clink");
                  }
                 //事件过滤器
                 foreach (QPushButton *btn, btnlist_ui) {
                     btn->installEventFilter(this);
                 }

                 QGridLayout *theLayout = new QGridLayout();
//                 theLayout->setAlignment(Qt::AlignCenter);
                 theLayout->addWidget(btnlist_ui.at(0), 0, 0);
                 theLayout->addWidget(btnlist_ui.at(1), 1, 0);
                 theLayout->addWidget(btnlist_ui.at(2), 2, 0);
                 theLayout->addWidget(btnlist_ui.at(3), 3, 0);
                 theLayout->addWidget(btnlist_ui.at(4), 4, 0);
                 theLayout->addWidget(btnlist_ui.at(5), 5, 0);
                 theLayout->addWidget(btnlist_ui.at(6), 6, 0);
                 theLayout->addWidget(theLabel[3], 0, 2);
                 theLayout->addWidget(theLabel[4], 1, 2);
                 theLayout->addWidget(theLabel[5], 2, 2);
                 theLayout->addWidget(theLabel[6], 3, 2);
                 theLayout->addWidget(theLabel[7], 4, 2);
                 theLayout->addWidget(theLabel[8], 5, 2);
                 theLayout->setContentsMargins(50,90,50,50);//(int left, int top, int right, int bottom)
                 theLayout->setSizeConstraint(QLayout::SetMinimumSize);
                   setLayout(theLayout);

                   QWidget* widget = new QWidget(this);
                   widget->setGeometry(0,0,640,330);
                   widget->setLayout(theLayout);
}


void Port_Menu::connect_Init()
{

    //定时器
    connect(timer,&QTimer::timeout,[=](){
//        if(show_flag)
//        {
//            theLabel[1]->setText((Elec_Gas[elec_flag]));
//            show_flag =0 ;
//        }
//        else
//        {
//            str_show="";
//            for(int i=0;i<5;i++)
//            {
//                    str_show.append("  ");
//            }
//            theLabel[1]->setText(str_show);
//            show_flag=1;
//        }
    });

    //波特率1200-115200
    connect(btnlist_ui.at(0),&QPushButton::clicked,[=](){


        if(flag_bundrate<7)
            flag_bundrate++;
        else
            flag_bundrate=0;
        theLabel[3]->setText(bundrate_sel[flag_bundrate]);
        Sysset->CommuSet.Serial_Opt.boundrate = bundrate_sel[flag_bundrate].toInt();
        Sysctrl->Para_Save = 1;
        Sysctrl->Communicat_updata = 1;
    });
    //数据位7-8
    connect(btnlist_ui.at(1),&QPushButton::clicked,[=](){
        if(Sysset->CommuSet.Serial_Opt.databit ==8)
            Sysset->CommuSet.Serial_Opt.databit =7;
        else
            Sysset->CommuSet.Serial_Opt.databit=8;
//        theLabel[4]->setNum(Sysset->CommuSet.Serial_Opt.databit);
        Sysctrl->Para_Save = 1;
        Sysctrl->Communicat_updata = 1;
    });
    //奇偶校验  0无 1奇校验 2偶校验
    connect(btnlist_ui.at(2),&QPushButton::clicked,[=](){
        if(Sysset->CommuSet.Serial_Opt.parity==0)
        {
            theLabel[5]->setText(tr("奇校验"));
            Sysset->CommuSet.Serial_Opt.parity=1;
        }
        else if(Sysset->CommuSet.Serial_Opt.parity==1)
        {
            theLabel[5]->setText(tr("偶校验"));
            Sysset->CommuSet.Serial_Opt.parity=2;
        }
        else
        {
            theLabel[5]->setText(tr("无"));
            Sysset->CommuSet.Serial_Opt.parity=0;
        }
        Sysctrl->Para_Save = 1;
        Sysctrl->Communicat_updata = 1;
    });
    //停止位1-2
    connect(btnlist_ui.at(3),&QPushButton::clicked,[=](){
        if(Sysset->CommuSet.Serial_Opt.stopbit==1)
            Sysset->CommuSet.Serial_Opt.stopbit=2;
        else
            Sysset->CommuSet.Serial_Opt.stopbit = 1;
        theLabel[6]->setText(QString::number(Sysset->CommuSet.Serial_Opt.stopbit));
        Sysctrl->Para_Save = 1;
        Sysctrl->Communicat_updata = 1;
    });
    //协议选择232/485   0 232, 1 485
    connect(btnlist_ui.at(4),&QPushButton::clicked,[=](){
        if(Sysset->CommuSet.Com_Bus_Sel)
        {
            Sysset->CommuSet.Com_Bus_Sel = 0;
            theLabel[7]->setText("RS-232");
        }
        else
        {
            Sysset->CommuSet.Com_Bus_Sel = 1;
            theLabel[7]->setText("RS-485");
        }
        Sysctrl->Para_Save = 1;
        Sysctrl->Communicat_updata = 1;

    });

//通讯协议 0-clink 1-modbus
    connect(btnlist_ui.at(5),&QPushButton::clicked,[=](){
        if(Sysset->CommuSet.communicate_procotol==0)
        {
            Sysset->CommuSet.communicate_procotol=1;  //通讯协议 0-clink 1-modbus
            theLabel[8]->setText("modbus");
        }
        else
        {
            Sysset->CommuSet.communicate_procotol = 0;
            theLabel[8]->setText("clink");
        }

    });
    //返回按钮
    connect(btnlist_ui.at(6),&QPushButton::clicked,[=](){
        this->parentWidget()->show();
        this->close();
        Sysctrl->Para_Save = 1;
    });




}

bool Port_Menu::eventFilter(QObject *watched, QEvent *event)
    {

            for(int i=0;i<btnlist_ui.length();i++)
                if(btnlist_ui.at(i) == watched)
            {
                if(event->type() == QEvent::KeyPress)
                {
                   QKeyEvent *ev = (QKeyEvent *)event;
                   QPushButton *button = (QPushButton *)watched;
                   switch (ev->key())
                   {
                   case OK:
                     {
                       emit  button->clicked();
                       break;
                     }
                   case UP://上
                   {
                       if(focusWidget() != btnlist_ui.first())
                       focusPreviousChild();
                       break;
                   }
                   case DOWN://下
                   {
                       if(focusWidget() != btnlist_ui.back())
                       focusNextChild();
                       break;
                   }
                   case LEFT://左
                   {
                     if(focusWidget() != btnlist_ui.first())
                          focusPreviousChild();
                     break;
                   }
                   case RIGHT://右
                   {
                     if(focusWidget() != btnlist_ui.back())
                          focusNextChild();
                  break;
                   }
                   case INIT: //打开初始化界面
                   {
                       init_Show *show = new init_Show(this);
                       show->show();
                       this->hide();
                       break;
                   }
                   case BACK:
                   {
                       this->parentWidget()->show();
                       this->close();
                       break;
                   }
                   default:
                       break;
                   }
                }
//                else if (event->type()==QEvent::FocusIn)     //然后再判断控件的具体事件 (这里指获得焦点事件)
//                          {
//                              QPushButton *focus_button = (QPushButton *)watched;
//                                      focus_button->setFixedSize(160,60);
//                          }
//                else if (event->type()==QEvent::FocusOut)    // 这里指 控件的失去焦点事件
//                         {
//                    QPushButton *focus_button = (QPushButton *)watched;
//                            focus_button->setFixedSize(120,50);
//                         }
                }
           return QMainWindow::eventFilter(watched,event);//返回父类的事件过滤器

    }
